package com.mapler.engine.linkbucks;

import com.mapler.engine.adf.*;
import com.mapler.tbot.ui.*;
import com.mapler.ui.*;
import com.mapler.model.SModel;
import com.mapler.service.INotifier;
import com.mapler.tbot.service.TBotWorker;
import com.mapler.utility.IConstant;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;

/**
 *
 * @author none
 */
public class EngineLinkBucksUI extends javax.swing.JPanel implements INotifier {

    public ScheduledExecutorService emailExecutor = Executors.newScheduledThreadPool(10);
    protected ScheduledFuture<?> emailFuture = null;
    private String username;
    private String password;
    private SModel sModel;
    private static Logger log = Logger.getLogger(EngineUI.class);
    private INotifier iNotifier;
    private LinkBucksWorker adfWorker;

    public EngineLinkBucksUI(String username, String password) {
        this.username = username;
        this.password = password;
        initComponents();
        this.vpn.setSelected(true);
        initSettings();
    }

    public SModel getsModel() {
        if (sModel == null) {
            sModel = new SModel();
        }
        return sModel;
    }

    public void setsModel(SModel sModel) {
        this.sModel = sModel;
    }

    /**
     * Creates new form EngineUI
     */
    public EngineLinkBucksUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        start = new javax.swing.JButton();
        end = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        driver = new javax.swing.JComboBox();
        vpn = new javax.swing.JCheckBox();
        message = new javax.swing.JLabel();
        jTFLink = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTFProxy = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        vpnusername = new javax.swing.JTextField();
        vpnpassword = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTFMaxWaitTime = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jBStop = new javax.swing.JButton();
        jCBHideRes = new javax.swing.JCheckBox();
        jCBClose = new javax.swing.JCheckBox();

        jLabel4.setText("jLabel4");

        start.setText("Start");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        end.setText("Stop & Exit");
        end.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endActionPerformed(evt);
            }
        });

        jLabel3.setText("Browser");

        driver.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "FF", "IE", "GC" }));

        vpn.setText("VPN");

        jLabel1.setText("Link");

        jLabel2.setText("Proxy");

        jLabel5.setText("Username");

        jLabel6.setText("Password");

        jLabel7.setText("Max wait time");

        jBStop.setText("Stop");
        jBStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBStopActionPerformed(evt);
            }
        });

        jCBHideRes.setSelected(true);
        jCBHideRes.setText("Hide resources");

        jCBClose.setSelected(true);
        jCBClose.setText("Close after page load");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(message, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(143, 143, 143)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(driver, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(start)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBStop)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(end))
                            .addComponent(vpnpassword)
                            .addComponent(vpnusername)
                            .addComponent(jTFMaxWaitTime, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(vpn)
                            .addComponent(jCBClose)
                            .addComponent(jCBHideRes)
                            .addComponent(jTFLink, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
                            .addComponent(jTFProxy))
                        .addGap(0, 38, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTFLink, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTFProxy, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vpnusername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vpnpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(driver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vpn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFMaxWaitTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCBHideRes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCBClose)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(start)
                    .addComponent(end)
                    .addComponent(jBStop))
                .addGap(59, 59, 59))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
        if (this.getsModel() == null) {
            notify("Setting is empty");
            return;
        }

        this.getsModel().setDriver(this.driver.getSelectedItem().toString());
        this.getsModel().setIsVPN(vpn.isSelected());
        
        if(jTFLink.getText() != null && !jTFLink.getText().isEmpty()) {
           this.getsModel().setLink(jTFLink.getText());
        }
        
        if(jTFProxy.getText() != null && !jTFProxy.getText().isEmpty()) {
            String[] proxyes = jTFProxy.getText().split(",");
            for( String proxy : proxyes ){
                if(!proxy.isEmpty()){
                    this.getsModel().getProxyes().add(proxy.trim());
                }
            }
        }
        
        this.getsModel().setName(this.vpnusername.getText());
        this.getsModel().setPass(this.vpnpassword.getText());
        this.getsModel().setStopEngine(false);
        this.getsModel().setCloseJustAfterLoad(jCBClose.isSelected());
        this.getsModel().setResourcesHide(jCBHideRes.isSelected());
                
        sModel.setMaxWaitTime(IConstant.MAXWAITTIME.toString());
        if (jTFMaxWaitTime.getText() != null && !jTFMaxWaitTime.getText().isEmpty()){
            sModel.setMaxWaitTime(jTFMaxWaitTime.getText());
        } else {
            sModel.setMaxWaitTime("1");
        }        
        sModel.setUsername(username);
        sModel.setPassword(password);

        adfWorker = new LinkBucksWorker(this.getiNotifier(), this.getsModel());
        adfWorker.update();
    }//GEN-LAST:event_startActionPerformed

    private void endActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endActionPerformed
        try {            
            Runtime.getRuntime().exec("taskkill /F /IM java.exe");
          
        } catch (Throwable ex) {
            log.debug("Error " + ex);
        }
    }//GEN-LAST:event_endActionPerformed

    private void jBStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBStopActionPerformed
       this.getsModel().setStopEngine(true);
    }//GEN-LAST:event_jBStopActionPerformed

    public void initSettings() {
        this.setsModel(null);
    }

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox driver;
    private javax.swing.JButton end;
    private javax.swing.JButton jBStop;
    private javax.swing.JCheckBox jCBClose;
    private javax.swing.JCheckBox jCBHideRes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jTFLink;
    private javax.swing.JTextField jTFMaxWaitTime;
    private javax.swing.JTextField jTFProxy;
    private javax.swing.JLabel message;
    private javax.swing.JButton start;
    private javax.swing.JCheckBox vpn;
    private javax.swing.JTextField vpnpassword;
    private javax.swing.JTextField vpnusername;
    // End of variables declaration//GEN-END:variables

    @Override
    public void notify(final String info) {
        try {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    message.setText(info);
                }
            });
        } catch (Throwable ex) {
            log.debug("notify:: Error causes " + ex.getMessage());
        }
    }

    public INotifier getiNotifier() {
        if (iNotifier == null) {
            iNotifier = this;
        }
        return iNotifier;
    }

    public void setiNotifier(INotifier iNotifier) {
        this.iNotifier = iNotifier;
    }

    @Override
    public void write(String info) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
